
#include "sec_common.h"
#include "e_os.h"
#include "hard.h"
#include <openssl/modes.h>
static unsigned char key1_e[] = "\x01\x00\x01";

#define SM2_SIZE 32
#define ACLASS_SM2_ENCRYPT         0x41
#define ACLASS_SM2_DECRYPT         0x42
#define ACLASS_SM2_SIGNATURE       0x43
#define ACLASS_SM2_VERIFY	   0x44
#define ACLASS_SM2_GENKEY          0x45
#define ACLASS_SM2_SIGNATURE_NOID  0x46
#define ACLASS_SM2_VERIFY_NOID     0x47
#define ACLASS_SM2_GENKEY_SEED     0x4a
#define FP                         0x00

struct sm2_keygen_api
{
    unsigned int field;  /* FP, F2M */
    unsigned int plen;
    unsigned int nlen;
    unsigned int klen;
    unsigned int entla;
    unsigned int entlb;
    unsigned char p[SM2_SIZE];
    unsigned char a[SM2_SIZE];
    unsigned char b[SM2_SIZE];
    unsigned char gx[SM2_SIZE];
    unsigned char gy[SM2_SIZE];
    unsigned char n[SM2_SIZE];
    unsigned char pri_key[SM2_SIZE];
    unsigned char pub_x[SM2_SIZE];
    unsigned char pub_y[SM2_SIZE];
};

struct sm2_enc_api
{
    unsigned int  field;  /* FP, F2M */
    unsigned int  plen;
    unsigned int  nlen;
    unsigned int  klen;   /* msg len*/
    unsigned int  entla;  /* ida len */
    unsigned int  entlb;  /* idb len */
    unsigned char p[SM2_SIZE];
    unsigned char a[SM2_SIZE];
    unsigned char b[SM2_SIZE];
    unsigned char gx[SM2_SIZE];
    unsigned char gy[SM2_SIZE];
    unsigned char n[SM2_SIZE];
    unsigned char pub_x[SM2_SIZE];
    unsigned char pub_y[SM2_SIZE];
    unsigned char mes[SM2_SIZE];
    unsigned char c1[32*2+1];
    unsigned char c2[SM2_SIZE];
    unsigned char c3[32];
};

struct sm2_dec_api
{
    unsigned int  field;  /* FP, F2M */
    unsigned int  plen;
    unsigned int  nlen;
    unsigned int  klen;   /* msg len*/
    unsigned int  entla;  /* ida len */
    unsigned int  entlb;  /* idb len */
    unsigned char p[SM2_SIZE];
    unsigned char a[SM2_SIZE];
    unsigned char b[SM2_SIZE];
    unsigned char gx[SM2_SIZE];
    unsigned char gy[SM2_SIZE];
    unsigned char n[SM2_SIZE];
    unsigned char pri_key[SM2_SIZE];
    unsigned char c1[32*2+1];
    unsigned char c2[SM2_SIZE];
    unsigned char c3[32];
    unsigned char mes[SM2_SIZE];
};


struct sm2_sign_api
{
    unsigned int  field;  /* FP, F2M */
    unsigned int  plen;
    unsigned int  nlen;
    unsigned int  klen;   /* msg len*/
    unsigned int  entla;  /* ida len */
    unsigned int  entlb;  /* idb len */
    unsigned char p[SM2_SIZE];
    unsigned char a[SM2_SIZE];
    unsigned char b[SM2_SIZE];
    unsigned char gx[SM2_SIZE];
    unsigned char gy[SM2_SIZE];
    unsigned char n[SM2_SIZE];
    unsigned char pri_key[SM2_SIZE];
    unsigned char e[SM2_SIZE];
    unsigned char r[SM2_SIZE];
    unsigned char s[SM2_SIZE];
};


struct sm2_verify_api
{
    unsigned int  field;  /* FP, F2M */
    unsigned int  plen;
    unsigned int  nlen;
    unsigned int  klen;   /* msg len*/
    unsigned int  entla;  /* ida len */
    unsigned int  entlb;  /* idb len */
    unsigned char p[SM2_SIZE];
    unsigned char a[SM2_SIZE];
    unsigned char b[SM2_SIZE];
    unsigned char gx[SM2_SIZE];
    unsigned char gy[SM2_SIZE];
    unsigned char n[SM2_SIZE];
    unsigned char pub_x[SM2_SIZE];
    unsigned char pub_y[SM2_SIZE];
    unsigned char e[SM2_SIZE];
    unsigned char r[SM2_SIZE];
    unsigned char s[SM2_SIZE];
};

int ccp903_sm2_keygen(int ccp903_fd, unsigned char *wx, int *wxlen, unsigned char *wy, int *wylen, unsigned char *privkey, int *privkeylen);

int  ccp903_sm2_encrpy(int ccp903_fd, unsigned char *mesg, int mesg_len, unsigned char *pub_x, int xlen, unsigned char *pub_y, int ylen, unsigned char *c1,int *c1_len, unsigned char *c2, int *c2_len, unsigned char *c3, int *c3_len);

int  ccp903_sm2_decrypt(int ccp903_fd, unsigned char *pri_key, int pri_key_len, unsigned char *c1,int c1_len, unsigned char *c2, int c2_len, unsigned char *c3, int c3_len, unsigned char *mesg, int *mesg_len);

int ccp903_sm2_sign(int ccp903_fd, unsigned char *hash, int hashlen, unsigned char *privkey, int privkeylen, unsigned char *r, int *rlen, unsigned char *s, int *slen);


int  ccp903_sm2_verify(int ccp903_fd, unsigned char *hash, int hashlen, unsigned char  *r, int rlen, unsigned char *s, int slen, unsigned char *pub_x, int xlen, unsigned char *pub_y, int ylen);


